package com.sk.sourcecircle.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.mine.act.PreviewViewpagerActivity;
import com.sk.sourcecircle.entity.AlbumaBean;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.view.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sk on 2017/12/27.
 */

public class AblumListAdapter extends BaseQuickAdapter<AlbumaBean, BaseViewHolder> {

    public AblumListAdapter(int layoutResId, @Nullable List<AlbumaBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, AlbumaBean item) {
        helper.setText(R.id.my_name, item.getDetail().getNickname())
                .setText(R.id.tv_content, item.getRank()).setText(R.id.from_group, item.getDetail().getCommunity_name())
                .addOnClickListener(R.id.comment)
                .addOnClickListener(R.id.add_praise)
                .addOnClickListener(R.id.cancel_praise)
                .addOnClickListener(R.id.add_care)
                .addOnClickListener(R.id.cancel_care)
                .addOnClickListener(R.id.more)
                .addOnClickListener(R.id.tv_delete);

        if (item.getCreate_time() != null) {
            helper.setText(R.id.tv_time, MyUtils.getTimeFormatText(item.getCreate_time()) + "   来自");
        } else {
            helper.setText(R.id.tv_time, "    ");
        }

        if (MyUtils.getCode(mContext) == 0) {
            helper.setVisible(R.id.tv_delete, false);
        } else {
            if (MyUtils.getCode(mContext) == item.getUser_id()) {
                helper.setVisible(R.id.tv_delete, true);
            } else {
                helper.setVisible(R.id.tv_delete, false);
            }
        }

        Glide.with(mContext).load(item.getDetail().getHead_url()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into((ImageView) helper.getView(R.id.my_head));


        LinearLayout linearLayout = helper.getView(R.id.comment_ryc);
        FlowLayout flowLayout = helper.getView(R.id.flow_ll);
        linearLayout.removeAllViews();
        flowLayout.removeAllViews();

        if (item.getDetail().getIs_dig() == 1) {
            helper.setVisible(R.id.cancel_praise, true)
                    .setVisible(R.id.add_praise, false);
        } else {
            helper.setVisible(R.id.cancel_praise, false)
                    .setVisible(R.id.add_praise, true);
        }

        if (item.getDetail().getIs_friend() == 1) {
            helper.setVisible(R.id.cancel_care, true)
                    .setVisible(R.id.add_care, false);
        } else {
            helper.setVisible(R.id.cancel_care, false)
                    .setVisible(R.id.add_care, true);
        }

        List<AlbumaBean.DetailBean.ReplyBean> replyBeanList = item.getDetail().getReply();


        if (replyBeanList != null && replyBeanList.size() > 0) {

            List<AlbumaBean.DetailBean.ReplyBean> replyList = new ArrayList<>();
            List<AlbumaBean.DetailBean.ReplyBean> praiseList = new ArrayList<>();

            for (AlbumaBean.DetailBean.ReplyBean data : replyBeanList) {
                if (data.getType().equals("reply")) {
                    replyList.add(data);
                } else if (data.getType().equals("dig")) {
                    praiseList.add(data);
                }
            }

            if (replyList.size() > 1) {
                helper.setGone(R.id.more, true);

                //全部
                if (item.isExpand()) {
                    helper.setGone(R.id.more, false);
                    for (AlbumaBean.DetailBean.ReplyBean data : replyList) {

                        TextView tv_comment = new TextView(mContext);
                        String name = data.getNickname() + ":";
                        String conetent = data.getContent();
                        int nameLong = name.length();
                        int contentLong;
                        if (conetent != null) {
                            contentLong = conetent.length();
                        } else {
                            contentLong = 0;
                            conetent = "";
                        }
                        SpannableStringBuilder style = new SpannableStringBuilder(name + conetent);
                        style.setSpan(new ForegroundColorSpan(Color.parseColor("#238cf5")), 0, nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        style.setSpan(new ForegroundColorSpan(Color.parseColor("#101010")), nameLong, contentLong + nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv_comment.setText(style);
                        linearLayout.addView(tv_comment);

                    }
                } else {
                    //一条评论
                    helper.setGone(R.id.more, true);
                    TextView tv_comment = new TextView(mContext);
                    String name = replyList.get(0).getNickname() + ":";
                    String conetent = replyList.get(0).getContent();
                    int nameLong = name.length();
                    int contentLong;
                    if (conetent != null) {
                        contentLong = conetent.length();
                    } else {
                        contentLong = 0;
                        conetent = "";
                    }
                    SpannableStringBuilder style = new SpannableStringBuilder(name + conetent);
                    style.setSpan(new ForegroundColorSpan(Color.parseColor("#238cf5")), 0, nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    style.setSpan(new ForegroundColorSpan(Color.parseColor("#101010")), nameLong, contentLong + nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tv_comment.setText(style);
                    linearLayout.addView(tv_comment);
                }

            } else if (replyList.size() == 1) {
                helper.setGone(R.id.more, false);
                TextView tv_comment = new TextView(mContext);
                String name = replyList.get(0).getNickname() + ":";
                String conetent = replyList.get(0).getContent();
                int nameLong = name.length();
                int contentLong;
                if (conetent != null) {
                    contentLong = conetent.length();
                } else {
                    contentLong = 0;
                    conetent = "";
                }
                SpannableStringBuilder style = new SpannableStringBuilder(name + conetent);
                style.setSpan(new ForegroundColorSpan(Color.parseColor("#238cf5")), 0, nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                style.setSpan(new ForegroundColorSpan(Color.parseColor("#101010")), nameLong, contentLong + nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv_comment.setText(style);
                linearLayout.addView(tv_comment);
            } else {
                helper.setGone(R.id.more, false);
            }

            if (praiseList.size() > 0) {
                for (int i = 0; i < praiseList.size(); i++) {
                    AlbumaBean.DetailBean.ReplyBean data = praiseList.get(i);
                    TextView textView = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_dig, flowLayout, false);
                    if (i == praiseList.size() - 1) {
                        textView.setText(data.getNickname() + ":");
                    } else {
                        textView.setText(data.getNickname() + ",");
                    }
                    flowLayout.addView(textView);
                }
                TextView textView = new TextView(mContext);
                textView.setTextColor(Color.parseColor("#101010"));
                textView.setText("赞赏了一下");
                flowLayout.addView(textView);
            }

        } else {
            helper.setGone(R.id.more, false);
        }

        RecyclerView recyclerView = helper.getView(R.id.grid_view);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        final List<String> stringList = item.getImages();
        PictureAdapter adapter = new PictureAdapter(R.layout.item_ablum_picture, stringList);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, PreviewViewpagerActivity.class);
                Activity activity = (Activity) mContext;
                intent.putExtra("pos", position);
                intent.putStringArrayListExtra("list", (ArrayList<String>) stringList);
                activity.startActivity(intent);
            }
        });
    }
}
